/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.utils.LogUtils;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;


/**
 *
 * @author Akil Mahimwala
 */
@Component
public class DataErrorController extends DefaultHandlerExceptionResolver {

    @Override
    protected org.springframework.web.servlet.ModelAndView doResolveException(HttpServletRequest hsr, HttpServletResponse hsr1, Object o, Exception excptn) {
        LogUtils.systemLogger.info(LogUtils.buildStringForSystemLog(excptn));
        excptn.printStackTrace();
        Map<String, Object> exception = new HashMap<String, Object>();
        exception.put("message", excptn.getLocalizedMessage());
        StringWriter sWriter = new StringWriter();
        try {
        excptn.getCause().printStackTrace(new PrintWriter(sWriter));
        exception.put("cause", sWriter.toString());
        } catch(NullPointerException ne) {
            sWriter.append("No cause found.");
        }
        return new ModelAndView("/errors/data-error", "exception", exception);
    }

}
